package app.imttf.offline.backing.buatttf;

import app.imttf.offline.adfextensions.ADFUtils;
import app.imttf.offline.model.OfflineTtfAMImpl;
import app.imttf.offline.model.view.buatttf.InfoSupplierScanViewImpl;
import app.imttf.offline.model.view.buatttf.InfoSupplierScanViewRowImpl;

import app.imttf.offline.model.view.buatttf.TtfHeaderGetKeyViewImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderGetKeyViewRowImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderUpdateStatusViewImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderUpdateStatusViewRowImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderViewImpl;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Map;

import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.jbo.Row;

public class BuatTtfBackingBean {

    private String _action;
    private RichInputText itSupCode;
    private String keyTtf;
    private String keyFpBpb;
    private StringBuilder dateNowStr;
    private SimpleDateFormat keyDateFormat =
        new SimpleDateFormat("ddMMyyyyhhmmss");

    public BuatTtfBackingBean() {
    }

    public String buatTtfAction() {

        String supCode;
        String branchCode;
        String pkpNonFlag;
        String npwpSup;

        OfflineTtfAMImpl offlineTtfAM =
            (OfflineTtfAMImpl)ADFUtils.getApplicationModuleForDataControl("OfflineTtfAMDataControl");

        Date dateNow = new Date();
        dateNowStr = new StringBuilder(keyDateFormat.format(dateNow));

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        try {
            supCode = itSupCode.getValue().toString();
        } catch (Exception e) {
            supCode = "";
        }

        if (supCode != "") {

            branchCode = (String)pageFlowScope.get("kodeDc");
            pkpNonFlag = supplierType(offlineTtfAM, supCode).trim();
            npwpSup = supplierNpwp(offlineTtfAM, supCode).trim();

            // Generate key TTF
            keyTtf = generateKeyTtf(branchCode, supCode, dateNowStr);
            keyFpBpb = generateKeyFpBpb(keyTtf, dateNowStr, pkpNonFlag);

            // Create new TTF Header
            // PARAM: offlineTtfAM, SupSiteCode, Npwp, KeyTtf, BranchCode
            createNewHeader(offlineTtfAM, supCode, npwpSup, keyTtf,
                            branchCode);

            System.out.println("-------------------------");
            System.out.println("KEY TTF   : " + keyTtf);
            System.out.println("KEY FP BPB: " + keyFpBpb);
            System.out.println("-------------------------");

            // Put variable into pageflow
            pageFlowScope.put("supCode", supCode);
            pageFlowScope.put("keyTtf", keyTtf);
            pageFlowScope.put("keyFpBpb", keyFpBpb);
            pageFlowScope.put("tipeSup", pkpNonFlag);
            pageFlowScope.put("npwpSup", npwpSup);

            _action = "newTtf";
        } else {
            /*
            System.out.println("-------------------------------------");
            System.out.println("kode supplier tidak boleh kosong");
            System.out.println("-------------------------------------");
            */
            _action = null;
        }

        return _action;

    }

    public void setItSupCode(RichInputText itSupCode) {
        this.itSupCode = itSupCode;
    }

    public RichInputText getItSupCode() {
        return itSupCode;
    }

    private String generateKeyTtf(String branchCode, String supCode,
                                  StringBuilder dateNowStr) {
        String keyTtf;

        // Key TTF format: (BRANCH CODE)(SUP CODE)(DDMMYYYYHHMISS)
        keyTtf = branchCode.concat(supCode.concat(dateNowStr.toString()));

        return keyTtf;

    }

    private String generateKeyFpBpb(String keyTtf, StringBuilder dateNowStr,
                                    String pkpNonFlag) {
        String keyFbBpb;

        // Key FP - BPB format: (BRANCH CODE)(SUP CODE)(DDMMYYYYHHMISS)(FB)(DDMMYYYYHHMISS)
        if (pkpNonFlag.equalsIgnoreCase("Y")) {
            keyFbBpb = keyTtf.concat("FB".concat(dateNowStr.toString()));
        } else {
            keyFbBpb = keyTtf.concat("FB0000000000000000000");
        }

        return keyFbBpb;
    }

    private String supplierType(OfflineTtfAMImpl offlineTtfAM,
                                String supCode) {
        String tipeSup;

        InfoSupplierScanViewImpl infoSupplierView =
            (InfoSupplierScanViewImpl)offlineTtfAM.getInfoSupplierScanView1();
        infoSupplierView.setsupCd(supCode);
        infoSupplierView.executeQuery();

        InfoSupplierScanViewRowImpl infoSuppRow =
            (InfoSupplierScanViewRowImpl)infoSupplierView.first();
        tipeSup = infoSuppRow.getFlagPkp();

        return tipeSup;
    }

    private String supplierNpwp(OfflineTtfAMImpl offlineTtfAM,
                                String supCode) {
        String supNpwp;

        InfoSupplierScanViewImpl infoSupplierView =
            (InfoSupplierScanViewImpl)offlineTtfAM.getInfoSupplierScanView1();
        infoSupplierView.setsupCd(supCode);
        infoSupplierView.executeQuery();

        InfoSupplierScanViewRowImpl infoSuppRow =
            (InfoSupplierScanViewRowImpl)infoSupplierView.first();
        supNpwp = infoSuppRow.getNoPkp();

        return supNpwp;
    }

    private void createNewHeader(OfflineTtfAMImpl offlineTtfAM,
                                 String SupSiteCode, String Npwp,
                                 String KeyTtf, String BranchCode) {

        java.sql.Timestamp timeStamp =
            new java.sql.Timestamp(System.currentTimeMillis());
        oracle.jbo.domain.Date dateTime =
            new oracle.jbo.domain.Date(timeStamp);

        TtfHeaderViewImpl ttfHeaderView = offlineTtfAM.getTtfHeaderView1();
        Row ttfHeaderRow = ttfHeaderView.createRow();
        ttfHeaderRow.setAttribute("TtfDate", dateTime);
        ttfHeaderRow.setAttribute("SupSiteCode", SupSiteCode);
        ttfHeaderRow.setAttribute("Npwp", Npwp);
        ttfHeaderRow.setAttribute("Status", "DRAFT");
        ttfHeaderRow.setAttribute("KeyTtf", KeyTtf);
        ttfHeaderRow.setAttribute("BranchCode", BranchCode);
        ttfHeaderView.insertRow(ttfHeaderRow);

    }

    public void noTtfToPageflow() {

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        String keyTtf = (String)pageFlowScope.get("keyTtf");

        OfflineTtfAMImpl offlineTtfAM =
            (OfflineTtfAMImpl)ADFUtils.getApplicationModuleForDataControl("OfflineTtfAMDataControl");

        TtfHeaderGetKeyViewImpl ttfHeaderGetKeyView =
            offlineTtfAM.getTtfHeaderGetKeyView1();
        ttfHeaderGetKeyView.setkeyTtf(keyTtf);
        ttfHeaderGetKeyView.executeQuery();

        TtfHeaderGetKeyViewRowImpl ttfHeaderGetKeyRow =
            (TtfHeaderGetKeyViewRowImpl)ttfHeaderGetKeyView.first();
        String numTtf = ttfHeaderGetKeyRow.getTtfNum();

        pageFlowScope.put("numTtf", numTtf);

    }


    public void updateTtfStatusReview() {

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        String keyTtf = (String)pageFlowScope.get("keyTtf");

        OfflineTtfAMImpl offlineTtfAM =
            (OfflineTtfAMImpl)ADFUtils.getApplicationModuleForDataControl("OfflineTtfAMDataControl");

        TtfHeaderUpdateStatusViewImpl ttfHeaderUpdateStatusView =
            offlineTtfAM.getTtfHeaderUpdateStatusView1();
        ttfHeaderUpdateStatusView.setkeyTtf(keyTtf);
        ttfHeaderUpdateStatusView.executeQuery();

        long ttfHeaderUpdateStatusNumRow =
            ttfHeaderUpdateStatusView.getEstimatedRowCount();

        if (ttfHeaderUpdateStatusNumRow > 0) {
            TtfHeaderUpdateStatusViewRowImpl ttfHeaderUpdateStatusRow =
                (TtfHeaderUpdateStatusViewRowImpl)ttfHeaderUpdateStatusView.first();
            ttfHeaderUpdateStatusRow.setStatus("REVIEW");      
            offlineTtfAM.getDBTransaction().commit();
        }
    }

    public void updateTtfStatusCancel() {

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        String keyTtf = (String)pageFlowScope.get("keyTtf");

        OfflineTtfAMImpl offlineTtfAM =
            (OfflineTtfAMImpl)ADFUtils.getApplicationModuleForDataControl("OfflineTtfAMDataControl");

        TtfHeaderUpdateStatusViewImpl ttfHeaderUpdateStatusView =
            offlineTtfAM.getTtfHeaderUpdateStatusView1();
        ttfHeaderUpdateStatusView.setkeyTtf(keyTtf);
        ttfHeaderUpdateStatusView.executeQuery();

        long ttfHeaderUpdateStatusNumRow =
            ttfHeaderUpdateStatusView.getEstimatedRowCount();

        if (ttfHeaderUpdateStatusNumRow > 0) {
            TtfHeaderUpdateStatusViewRowImpl ttfHeaderUpdateStatusRow =
                (TtfHeaderUpdateStatusViewRowImpl)ttfHeaderUpdateStatusView.first();
            ttfHeaderUpdateStatusRow.setStatus("CANCEL");        
            offlineTtfAM.getDBTransaction().commit();
        }
    }
}
